define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall_order_refund/index' + location.search,
                    import_url: 'mall_order_refund/import',
                    table: 'mall_order_refund',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'mallorder.sn', title: __('Order.sn'), operate: 'LIKE'},
                        {field: '', title: __('Project'),
                            formatter: function(row,data){
                                console.log(row,data)
                                let html = '<div data-sn="'+data.sn+'" data-id="'+data.id+'"><img width=30px" src="'+data.mallordergoods.goods_image+'">'+data.mallordergoods.goods_name+'<br>'+data.mallordergoods.type_txt+'</div>'
                                return html
                            }},
                        {field: 'mallordergoods.unit_price', title: __('Goods.unit_price'), operate:'BETWEEN'},
                        {field: 'mallordergoods.goods_spec_name', title: __('Goods.goods_spec_name'), operate: 'LIKE'},
                        {field: 'mallordergoods.buy_nums', title: __('Goods.buy_nums')},
                        {field: '', title: __('User_info'),
                            formatter: function(row,data){
                                console.log(row,data)
                                let html = '<div data-sn="'+data.sn+'" data-id="'+data.id+'">'+data.user.username+'<br>'+data.user.mobile+'</div>'
                                return html
                            }},
                        {field: 'refund_amount', title: __('Refund_amount'), operate:'BETWEEN'},
                        {field: 'total_refund_amount', title: __('Total_refund_amount'), operate:'BETWEEN'},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'reason', title: __('Reason'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.normal},
                        {field: 'after_sales_status', title: __('After_sales_status'), searchList: {"1":__('After_sales_status 1'),"2":__('After_sales_status 2'),"3":__('After_sales_status 3'),"4":__('After_sales_status 4'),"5":__('After_sales_status 5')}, formatter: Table.api.formatter.normal},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            events: {
                                'click .btn-duplicate1': function (e, value, row) {
                                    Layer.prompt({
                                        title: "请填写审核备注",
                                        success: function (layero) {
                                            $("input", layero).prop("placeholder", "备注");
                                        }
                                    }, function (value) {
                                        Fast.api.ajax({
                                            url: "mall_order_refund/changeStatus/ids/" + row.id,
                                            data: {admin_note: value, status: "1"},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                            //return false;
                                        });
                                    });
                                    return false;
                                },
                                'click .btn-duplicate2': function (e, value, row) {
                                    Layer.prompt({
                                        title: "请填写审核备注",
                                        success: function (layero) {
                                            $("input", layero).prop("placeholder", "备注");
                                        }
                                    }, function (value) {
                                        Fast.api.ajax({
                                            url: "mall_order_refund/changeStatus/ids/" + row.id,
                                            data: {admin_note: value, status: "2"},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                            //return false;
                                        });
                                    });
                                    return false;
                                }
                            },
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    // icon: 'fa fa-magic',
                                    url: 'mall_order_refund/detail/ids/{ids}',
                                    extend: 'data-area=["100%","100%"]',
                                },
                                {
                                    name: 'success',
                                    text: __('Operate 1'),
                                    classname: 'btn btn-xs btn-warning btn-duplicate1',
                                    icon: 'fa fa-copy',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == "0")
                                            return true;
                                        else
                                            return false;
                                    }
                                },
                                {
                                    name: 'default',
                                    text: __('Operate 2'),
                                    classname: 'btn btn-xs btn-warning btn-duplicate2',
                                    icon: 'fa fa-copy',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == "0")
                                            return true;
                                        else
                                            return false;
                                    }
                                },
                            ], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        campus_index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall_order_refund/campus_index' + location.search,
                    import_url: 'mall_order_refund/import',
                    table: 'mall_order_refund',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'mallorder.sn', title: __('Order.sn'), operate: 'LIKE'},
                        {field: '', title: __('Project'),
                            formatter: function(row,data){
                                console.log(row,data)
                                let html = '<div data-sn="'+data.sn+'" data-id="'+data.id+'"><img width=30px" src="'+data.mallordergoods.goods_image+'">'+data.mallordergoods.goods_name+'<br>'+data.mallordergoods.type_txt+'</div>'
                                return html
                            }},
                        {field: 'mallordergoods.unit_price', title: __('Goods.unit_price'), operate:'BETWEEN'},
                        {field: 'mallordergoods.goods_spec_name', title: __('Goods.goods_spec_name'), operate: 'LIKE'},
                        {field: 'mallordergoods.buy_nums', title: __('Goods.buy_nums')},
                        {field: '', title: __('User_info'),
                            formatter: function(row,data){
                                console.log(row,data)
                                let html = '<div data-sn="'+data.sn+'" data-id="'+data.id+'">'+data.user.username+'<br>'+data.user.mobile+'</div>'
                                return html
                            }},
                        {field: 'refund_amount', title: __('Refund_amount'), operate:'BETWEEN'},
                        {field: 'total_refund_amount', title: __('Total_refund_amount'), operate:'BETWEEN'},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'reason', title: __('Reason'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.normal},
                        {field: 'after_sales_status', title: __('After_sales_status'), searchList: {"1":__('After_sales_status 1'),"2":__('After_sales_status 2'),"3":__('After_sales_status 3'),"4":__('After_sales_status 4'),"5":__('After_sales_status 5')}, formatter: Table.api.formatter.normal},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            events: {
                                'click .btn-duplicate1': function (e, value, row) {
                                    Layer.prompt({
                                        title: "请填写审核备注",
                                        success: function (layero) {
                                            $("input", layero).prop("placeholder", "备注");
                                        }
                                    }, function (value) {
                                        Fast.api.ajax({
                                            url: "mall_order_refund/changeStatus/ids/" + row.id,
                                            data: {admin_note: value, status: "1"},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                            //return false;
                                        });
                                    });
                                    return false;
                                },
                                'click .btn-duplicate2': function (e, value, row) {
                                    Layer.prompt({
                                        title: "请填写审核备注",
                                        success: function (layero) {
                                            $("input", layero).prop("placeholder", "备注");
                                        }
                                    }, function (value) {
                                        Fast.api.ajax({
                                            url: "mall_order_refund/changeStatus/ids/" + row.id,
                                            data: {admin_note: value, status: "2"},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                            //return false;
                                        });
                                    });
                                    return false;
                                }
                            },
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    // icon: 'fa fa-magic',
                                    url: 'mall_order_refund/detail/ids/{ids}',
                                    extend: 'data-area=["100%","100%"]',
                                },
                                {
                                    name: 'success',
                                    text: __('Operate 1'),
                                    classname: 'btn btn-xs btn-warning btn-duplicate1',
                                    icon: 'fa fa-copy',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == "0")
                                            return true;
                                        else
                                            return false;
                                    }
                                },
                                {
                                    name: 'default',
                                    text: __('Operate 2'),
                                    classname: 'btn btn-xs btn-warning btn-duplicate2',
                                    icon: 'fa fa-copy',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == "0")
                                            return true;
                                        else
                                            return false;
                                    }
                                },
                            ], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            $(document).on('click','.changeStatus' ,function (e) {
                console.log( $(this).data('id'))
                console.log( $(this).data('this'))
                var id = $(this).data('id');
                var status = $(this).data('status');
                if(status == 1){
                    var title = "同意将直接退款到原支付账户";
                }else if(status == 2){
                    var title = "请填写备注";
                }
                Layer.prompt({
                    title: title,
                    success: function (layero) {
                        $("input", layero).prop("placeholder", "备注");
                    }
                }, function (value) {
                    Fast.api.ajax({
                        url: "mall_order_refund/changeStatus" ,
                        data: {admin_note: value, status: status,ids:id},
                    }, function (data, ret) {
                        Layer.closeAll();
                        $(".btn-refresh").trigger("click");
                        window.location.reload()
                        //return false;
                    });
                });
            })
            $(document).on('click','.takeGoods' ,function (e) {
                console.log( $(this).data('id'))
                console.log( $(this).data('type'))
                var id = $(this).data('id');
                var type = $(this).data('type');
                if(type == 2){
                    var title = "确认收货后将直接退款到原支付账户";
                }else{
                    var title = "请填写备注";
                }
                Layer.prompt({
                    title: title,
                    success: function (layero) {
                        $("input", layero).prop("placeholder", "备注");
                    }
                }, function (value) {
                    Fast.api.ajax({
                        url: "mall_order_refund/takeGoods" ,
                        data: {admin_note: value,ids:id},
                    }, function (data, ret) {
                        Layer.closeAll();
                        $(".btn-refresh").trigger("click");
                        window.location.reload()
                        //return false;
                    });
                });
            })
            $(document).on('click','.delivery' ,function (e) {
                console.log( $(this).data('id'))
                Layer.open({
                    type: 2,
                    content:'/admin/mall_order_refund/delivery?id='+$(this).data('id'),
                    area:['80%','90%'],
                    end:function(){
                        window.location.reload()
                    }
                })
            })
            Controller.api.bindevent();
        },
        delivery: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
